<?php

/*
 * This file is part of the overtrue/wechat.
 *
 * (c) overtrue <i@overtrue.me>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace hiduoke\services\workWechat\kf;

use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\Kernel\Exceptions\RuntimeException;
use EasyWeChat\Kernel\Messages\Message;
use EasyWeChat\Kernel\Messages\Text;
use EasyWeChat\Work\Kf\MessageClient;

/**
 * Class MessageBuilder.
 *
 * @author overtrue <i@overtrue.me>
 */
class Messenger
{
    /**
     * @var \EasyWeChat\Kernel\Messages\Message;
     */
    protected $message;

    /**
     * @var array
     */
    protected $open_kfid;

    protected $touser;

    protected $welcome_code;

    /**
     * @var MessageClient
     */
    protected $client;

    /**
     * Messenger constructor.
     * @param MessageClient $client
     */
    public function __construct(MessageClient $client)
    {
        $this->client = $client;
    }

    /**
     * Set message to send.
     *
     * @param string|Message $message
     *
     * @return Messenger
     *
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */

    public function message($message)
    {
        if (is_string($message) || is_numeric($message)) {
            $message = new Text($message);
        }

        if (!($message instanceof Message)) {
            throw new InvalidArgumentException('Invalid message.');
        }

        $this->message = $message;

        return $this;
    }


    /**
     * @param $touser
     * @return $this
     */
    public function toUser($touser)
    {
        $this->touser = $touser;
        return $this;
    }

    /**
     * @param $open_kfid
     * @return $this
     */
    public function toKfId($open_kfid)
    {
        $this->open_kfid = $open_kfid;
        return $this;
    }

    /**
     * @param $open_kfid
     * @return $this
     */
    public function toWelcomeCode($welcome_code)
    {
        $this->welcome_code = $welcome_code;
        return $this;
    }


    /**
     * @param \EasyWeChat\Kernel\Messages\Message|string|null $message
     * @return mixed
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    public function send($message = null)
    {
        if ($message) {
            $this->message($message);
        }

        if (empty($this->message)) {
            throw new RuntimeException('No message to send.');
        }

        $message = $this->message->transformForJsonRequest([
            'open_kfid' => $this->open_kfid,
            'touser'    => $this->touser,
        ]);
        return $this->client->send($message);
    }

    /**
     * @param null $message
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws InvalidArgumentException
     * @throws RuntimeException
     */
    public function event($message = null)
    {
        if ($message) {
            $this->message($message);
        }

        if (empty($this->message)) {
            throw new RuntimeException('No message to send.');
        }

        $message = $this->message->transformForJsonRequest([
            'code' => $this->welcome_code
        ]);
        return $this->client->event($message);
    }

    /**
     * Return property.
     * @param string $property
     * @return mixed
     * @throws InvalidArgumentException
     */
    public function __get($property)
    {
        if (property_exists($this, $property)) {
            return $this->$property;
        }

        throw new InvalidArgumentException(sprintf('No property named "%s"', $property));
    }
}
